def useradd():
    with open("passwd.txt", 'a') as fp:
        user = input("请输入你创建的用户名：")
        paw = input("请输入你要创建的密码：")
        fp.write(user + '\n' + paw)


def login(user, paw):
    with open("passwd.txt") as fp:
        list = fp.readlines()
        for i in range(0, len(list) - 1):
            if user + '\n' == list[i] and paw == list[i + 1]:
                print("登陆成功！")
                break
            else:
                print("登陆失败！")
    money = float(input("请输入购物预算："))
    return money


def meun_list():
    menu = {
        'F00001': {
            'name': '苹果',
            'price': 1.2
        },
        'F00002': {
            'name': '香蕉',
            'price': 5.5
        }
    }
    return menu


def meun_price(buy, meun, money):
    while True:
        buy_num = input("请输入要购买的商品编号：")
        if buy_num in meun:
            buy_qua = float(input("请输入要购买的商品数量："))
            if buy_qua * meun[buy_num]['price'] > money:
                print("用户的余额不够！")
            elif buy_num in buy:
                buy[buy_num] += buy_qua
                money -= buy_qua * meun[buy_num]['price']
            else:
                buy[buy_num] = buy_qua
                money -= buy_qua * meun[buy_num]['price']
            break
        else:
            print("商品编号错误，请重新输入！")
        input("请按回车...")
    return money


def main():
    print("欢迎来到sanle购物系统")
    print("用户登陆")
    user = input("请用户输入你的用户名: ")
    paw = input("请输入你的密码: ")
    money = login(user, paw)
    input("请按回车...")
    buy = {}
    while True:
        print("#############")
        print("1,显示商品信息")
        print("2,显示已购买信息")
        print("3,购买商品")
        print("4,输入exit退出")
        print("#############")
        num = input()
        if num == "exit":
            money1 = 0
            for key in buy:
                print(f"用户购买的商品为{meun_list()[key]['name']} 价格为{meun_list()[key]['price']} 数量为{buy[key]} "
                      f"总价为{meun_list()[key]['price'] * buy[key]}")
                money1 += meun_list()[key]['price'] * buy[key]
            print(f"购物金额为：{money1}")
            print(f"用户余额为：{float(money)}")
            break
        elif int(num) == 1:
            for key in meun_list():
                print(f"{key}  {meun_list()[key]}")
            input("请按回车...")
        elif int(num) == 2:
            print(buy)
            input("请按回车...")
        elif int(num) == 3:
            money = meun_price(buy, meun_list(), money)
            print(buy)
            input("请按回车...")


main()
